(function() {


    var form = document.getElementById('form'); //获取form

    var phone = form.phone; //获取手机号

    var pass1 = form.pass1; //创建密码

    var pass2 = form.pass2; //确认密码

    var ver1 = form.ver1; //图形验证码

    var ver2 = form.ver2; //短信验证

    var btn = form.btn; //注册按钮

    var arr = [];

    var verifyCode = new GVerify('ver_code'); //生成验证码

    var re = {
        phone: /^1\d{10}$/, //手机正则
        pass: /^\w{6,20}$/
    }


    // 手机号验证

    phone.onblur = function() {
            var val = this.value;
            var em = this.parentNode.querySelector('em') //获取em
            if (re.phone.test(val)) {
                U.addClass(em, 'ok');
                em.innerHTML = '';
                arr[0] = true;
            } else {
                U.removeClass(em, 'ok');
                em.innerHTML = '请输入正确的用户名'
                arr[0] = false;
            }
            if (val == '') {
                em.innerHTML = ''
                arr[0] = false;
            }
        }
        //    密码验证

    pass1.onblur = function() {
        var pass1_val = this.value;
        var em = this.parentNode.querySelector('em') //获取em
        if (re.pass.test(pass1_val)) {
            U.addClass(em, 'ok');
            em.innerHTML = '';
            arr[1] = true;
        } else {
            U.removeClass(em, 'ok');
            em.innerHTML = '6-20位字母数字下划线'
            arr[1] = false;
        }
        if (pass1_val == '') {
            em.innerHTML = ''
            arr[1] = false;
        }
    }

    //    再次确认密码

    pass2.onblur = function() {
            var pass1_val = pass1.value;
            var pass2_val = this.value;
            var em = this.parentNode.querySelector('em') //获取em
            if (pass1_val == pass2_val & pass1_val) {
                U.addClass(em, 'ok');
                em.innerHTML = '';
                arr[2] = true;
            } else {
                U.removeClass(em, 'ok');
                em.innerHTML = '两次密码不一样'
                arr[2] = false;
            }
            if (pass2_val == '') {
                em.innerHTML = ''
                arr[2] = false;
            }
        }
        //    验证码确认
    ver1.onblur = function() {
        var code1 = this.value;
        var em = this.parentNode.querySelector('em') //获取em
        if (verifyCode.validate(code1)) {
            U.addClass(em, 'ok');
            em.innerHTML = '';
            arr[3] = true;
        } else {
            U.removeClass(em, 'ok');
            em.innerHTML = '验证码不正确'
            arr[3] = false;
        }
        if (code1 == '') {
            em.innerHTML = ''
            arr[3] = false;
        }
    }

    ver2.onblur = function() {
        var code2 = this.value;
        var em = this.parentNode.querySelector('em') //获取em
        if (Number(code2) == 6666) {
            U.addClass(em, 'ok');
            em.innerHTML = '';
            arr[4] = true;

        } else {
            U.removeClass(em, 'ok');
            em.innerHTML = '验证码不正确'
            arr[4] = false;
        }
        if (code2 == '') {
            em.innerHTML = '提示: 6666'
            arr[4] = false;
        }
    }


    form.onsubmit = function() {
        phone.onblur();
        pass1.onblur();
        pass2.onblur();
        ver1.onblur();
        ver2.onblur();
        var sta = arr.every(function(itme) {
            return itme;
        })

        var str = 'val=' + phone.value + '&pass=' + pass1.value;
        console.log(str);

        if (sta) {
            U.ajax('get', './data/live.json', str, function(data) {
                console.log(JSON.parse(data));
            })
        }
        localStorage.setItem('phone', phone.value)




        setInterval(function() {
            window.location.href = "./index.html"
        }, 3000)



        //    禁止默认事件
        return false;
    }



})()